
***SET DIRECTORY***
global main "[YOUR PATH HERE]"
global output "${main}/output"


local switch_all = 0
local switch_nponly  = 0
local switch_fponly  = 0
	local switch_pubonly = 1
	local switch_menonly = 0
	local switch_womenonly = 1
	local switch_msu = 0
	local switch_urban = 0
	local switch_post90 = 1
	local switch_post00 = 0
	local switch_2yr = 0
	
local switch_controls = 0

********************************************************************************
**COMPLETIONS DATA**
********************************************************************************

use "${main}/data/outcomes_completions_cip_all.dta", clear
	drop _merge

merge m:1 file_year unitid using "${main}/data/school_types.dta", keep(match)
	

local type ""

if `switch_nponly'{
	drop if control == 3
	local type  "_nponly`type'"
}
if `switch_fponly'{
	keep if control == 3
	local type  "_fponly`type'"
}
if `switch_pubonly'{
	keep if control == 1
	local type  "_public`type'"
}
if `switch_menonly'{
	replace ctotalt = ctotalm
	local type  "_menonly`type'"
}
if `switch_womenonly'{
	replace ctotalt = ctotalw
	local type  "_womenonly`type'"
}
if `switch_msu'{
	keep if msu
	local type  "_msu`type'"
}
if `switch_urban'{
	keep if urban
	local type  "_urban`type'"
}
if `switch_post90'{
	keep if file_year >= 1990
	local type  "_`type'_post90"
}
if `switch_post00'{
	keep if file_year >= 2000
	local type  "_`type'_post00"
}
if `switch_2yr'{
	keep if schl_type == 1
	local type  "_`type'_2yronly"
}
else if `switch_all'{
	local type = "_all"
}

//drop BAs 
drop if deg == 5

//collapse by state
collapse (sum) ctotalt , by(file_year deg st_fips)
	rename ctotalt ctotal
	drop if st_fips > 56 //this drops all of the US territories
	drop if file_year < 1986 // this drops 1980, 1984 and 1985, for which our data are much spottier

if `switch_controls'{
	//generate indicators for if a state has a free CC program in a given year
	gen free_cc = 0
		replace free_cc = 1 if st_fips == 5  & file_year > 2016
		replace free_cc = 1 if st_fips == 6  & file_year > 2018
		replace free_cc = 1 if st_fips == 9  & file_year > 2019
		replace free_cc = 1 if st_fips == 10 & file_year > 2005
		replace free_cc = 1 if st_fips == 13 & file_year > 2013
		replace free_cc = 1 if st_fips == 15 & file_year > 2017
		replace free_cc = 1 if st_fips == 18 & file_year > 1990
		replace free_cc = 1 if st_fips == 19 & file_year > 2019
		replace free_cc = 1 if st_fips == 20 & file_year > 2016
		replace free_cc = 1 if st_fips == 21 & file_year > 2016
		replace free_cc = 1 if st_fips == 22 & file_year > 2016
		replace free_cc = 1 if st_fips == 24 & file_year > 2016
		replace free_cc = 1 if st_fips == 28 & file_year > 2016
		replace free_cc = 1 if st_fips == 29 & file_year > 2016
		replace free_cc = 1 if st_fips == 30 & file_year > 2016
		replace free_cc = 1 if st_fips == 32 & file_year > 2016
		replace free_cc = 1 if st_fips == 34 & file_year > 2016
		replace free_cc = 1 if st_fips == 36 & file_year > 2016
		replace free_cc = 1 if st_fips == 40 & file_year > 2016
		replace free_cc = 1 if st_fips == 41 & file_year > 2016
		replace free_cc = 1 if st_fips == 44 & file_year > 2016
		replace free_cc = 1 if st_fips == 46 & file_year > 2016
		replace free_cc = 1 if st_fips == 47 & file_year > 2016
		replace free_cc = 1 if st_fips == 53 & file_year > 2016
		replace free_cc = 1 if st_fips == 54 & file_year > 2016
		
	//add controls for unemployment rate by state
	preserve
		use "${main}/data/clean_controls_labormarket.dta", clear
		
		keep statefips unemp_rate year
			rename statefips st_fips
			rename year file_year
		
		tempfile unemployment
		save `unemployment'
	restore 
		
	merge m:1 file_year st_fips using `unemployment', assert(match using) keep(match) nogen 
		//need to assert match master so when we restrict years we can get rid of those not in our sample
			
	//add controls for log state population (18-30 years old)
	preserve
		use "${main}/data/census_yearly_statepop.dta", clear
			rename statefips st_fips
			rename year_academic file_year

		tempfile statepop
		save `statepop'
	restore 
	
	merge m:1 file_year st_fips using `statepop', assert(match using) keep(match) nogen 
		//need to assert match master so when we restrict years we can get rid of those not in our sample
		
	rename file_year year_academic
	rename st_fips statefips 
	keep year_academic statefips deg ctotal free_cc unemp_rate lnpop_1830
}
	
//rename and reshape
else{	
	rename file_year year_academic
	rename st_fips statefips 
	keep year_academic statefips deg ctotal
}

//here we combine 1 and 2 year certificates 
replace deg = 1 if deg == 2
	drop if deg == 4
		
reshape wide ctotal, i(year_academic statefips) j(deg)
	gen log_ctotal1 = ln(ctotal1)
	gen log_ctotal3 = ln(ctotal3)


//locals
local var log_ctotal  //ctotal
local ytitle "log degree completions"


********************************************************************************
**MINIMUM WAGE DATA**
********************************************************************************

merge 1:m year_academic statefips using "${main}/data/mw_stacked.dta", keep(match) // keep matches drops years outside of our enrollment panel


********************************************************************************
**GRAPHING**
********************************************************************************

matrix col1 = J(8,2,.)
matrix col3 = J(8,3,.)
matrix results = J(8,7,.)

* define treatment vector
global treat F*treat L*treat


*linear combinations of post-reg coeffs
global lincomm3 F3treat - F1treat
global lincomm2 F2treat - F1treat
global lincomm1 F1treat - F1treat
global lincom0 L0treat - F1treat
global lincom1 L1treat - F1treat
global lincom2 L2treat - F1treat
global lincom3 L3treat - F1treat
global lincom4 L4treat - F1treat


preserve
	eststo cert:  reghdfe `var'1 $treat , ///
			a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
			i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips)
				local dof = e(df_r)
				local i = 1
				foreach year in 3 2 1{
					lincom (${lincomm`year'})
						matrix col1[`i',1] = r(estimate)
						matrix col1[`i++',2] = r(se)
				}
				foreach year in 0 1 2 3 4{
					lincom (${lincom`year'})
						matrix col1[`i',1] = r(estimate)
						matrix col1[`i++',2] = r(se)
				}
	
	eststo aa:  reghdfe `var'3 $treat , ///
			a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
			i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips) 
				local i = 1
				foreach year in 3 2 1{
					lincom (${lincomm`year'})
						matrix col3[`i',1] = r(estimate)
						matrix col3[`i++',2] = r(se)
				}
				foreach year in 0 1 2 3 4{
					lincom (${lincom`year'})
						matrix col3[`i',1] = r(estimate)
						matrix col3[`i++',2] = r(se)
				}
			matrix col3[1,3] = -3
			matrix col3[2,3] = -2
			matrix col3[3,3] = -1
			matrix col3[4,3] = 0
			matrix col3[5,3] = 1
			matrix col3[6,3] = 2
			matrix col3[7,3] = 3
			matrix col3[8,3] = 4

	matrix results = col1, col3

	clear
	matlist results
	svmat results

	rename results1  coeff_1
	rename results2  se_1
	rename results3  coeff_3
	rename results4  se_3
	rename results5  time

	foreach x in 1 3{
		gen ci_upper_`x' = coeff_`x' + invttail(`dof', 0.025)*se_`x'
		gen ci_lower_`x' = coeff_`x' - invttail(`dof', 0.025)*se_`x'
	}


	//graphing style things
		grstyle init
		grstyle set plain
		grstyle set size 8pt: subheading axis_title

	//area plots
	twoway rarea ci_upper_1 ci_lower_1 time, color(ltblue%30) ||  ///
			line coeff_1 time, lcolor(ltblue) mcolor(ltblue) lp(dash) || ///
			rarea ci_upper_3 ci_lower_3 time, color(maroon%45) ||  ///
			line coeff_3 time, lcolor(maroon) mcolor(maroon)  ///
		ylab(-.3(.1).3, grid labsize(small)) yscale(range(-.32 .3)) yline(0, lp(dash) lc(black)) ///
		xline(-1, lp(dash) lc(black)) xlab(-3(1)4, grid labsize(small)) legend(col(3) ///
		order(2  "Certificates" 4 "AAs") ///
		size(small)) ytitle("`ytitle'") xtitle("years since minimum wage change") legend(region(lstyle(none)))
	graph export "${output}/MW`var'_${dmwlevel}`type'_area.pdf", replace


if `switch_controls'{
	preserve
		eststo certl1:  reghdfe `var'1 $treat free_cc unemp_rate, ///
				a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
				i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips)
					local dof = e(df_r)
					local i = 1
					foreach year in 3 2 1{
						lincom (${lincomm`year'})
							matrix col1[`i',1] = r(estimate)
							matrix col1[`i++',2] = r(se)
					}
					foreach year in 0 1 2 3 4{
						lincom (${lincom`year'})
							matrix col1[`i',1] = r(estimate)
							matrix col1[`i++',2] = r(se)
					}

		eststo aa:  reghdfe `var'3 $treat free_cc unemp_rate, ///
				a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
				i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips) 
					local i = 1
					foreach year in 3 2 1{
						lincom (${lincomm`year'})
							matrix col3[`i',1] = r(estimate)
							matrix col3[`i++',2] = r(se)
					}
					foreach year in 0 1 2 3 4{
						lincom (${lincom`year'})
							matrix col3[`i',1] = r(estimate)
							matrix col3[`i++',2] = r(se)
					}
				matrix col3[1,3] = -3
				matrix col3[2,3] = -2
				matrix col3[3,3] = -1
				matrix col3[4,3] = 0
				matrix col3[5,3] = 1
				matrix col3[6,3] = 2
				matrix col3[7,3] = 3
				matrix col3[8,3] = 4

		matrix results = col1, col3

		clear
		matlist results
		svmat results

		rename results1  coeff_1
		rename results2  se_1
		rename results3  coeff_3
		rename results4  se_3
		rename results5  time

		foreach x in 1 3{
			gen ci_upper_`x' = coeff_`x' + invttail(`dof', 0.025)*se_`x'
			gen ci_lower_`x' = coeff_`x' - invttail(`dof', 0.025)*se_`x'
		}


		//graphing style things
			grstyle init
			grstyle set plain
			grstyle set size 8pt: subheading axis_title

		//area plots
		twoway rarea ci_upper_1 ci_lower_1 time, color(ltblue%30)  ||  ///
				line coeff_1 time, lcolor(ltblue) mcolor(ltblue) lp(dash) || ///
				rarea ci_upper_3 ci_lower_3 time, color(maroon%45) ||  ///
				line coeff_3 time, lcolor(maroon) mcolor(maroon)  ///
			ylab(-.3(.1).3, grid labsize(small)) yscale(range(-.6 .3)) yline(0, lp(dash) lc(black)) xline(-1, lp(dash) lc(black)) ///
			xlab(-3(1)4, grid labsize(small)) legend(col(3) order(2  "Certificates" 4 "AAs") size(small)) legend(region(lstyle(none))) ///
			ytitle("`ytitle'") xtitle("years since minimum wage change")	
		graph export "${output}/MW`var'_${dmwlevel}`type'_controls_area.pdf", replace
			
	restore
}
		
